class Solution {
public:
    int square(int n)
    {
        int sum = 0;
        while (n > 0)
        {
            int a = n % 10;
            sum += a * a;
            n = n / 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int fast = square(n);
        fast = square(fast);
        int slow = square(n);
        while (fast != slow)
        {
            slow = square(slow);
            fast = square(fast);
            fast = square(fast);
        }
        if (fast == 1)
            return true;
        return false;
    }
};